﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Data.Services.Common;
using System.Runtime.Serialization;

namespace Sustainalytics.DynamicsCRMGateway.Entities
{
    [DataContractAttribute()]
    [EntityLogicalNameAttribute("new_companiesgroupconstituent")]
    [DataServiceKeyAttribute("Id")]
    [EntityAttribute("new_companiesgroupconstituentSet")]
    public class CompaniesGroupConstituent : CrmEntity, INameable
    {
        public CompaniesGroupConstituent() :
            base(EntityLogicalName)
        {
        }

        public const string EntityLogicalName = "new_companiesgroupconstituent";

        public const int EntityTypeCode = 10034;

        [AttributeLogicalNameAttribute("new_companiesgroupconstituentid")]
        public Nullable<Guid> CompaniesGroupConstituentId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_companiesgroupconstituentid"); }
            set { this.SetPrimaryIdAttributeValue<Nullable<Guid>>("new_companiesgroupconstituentId", "new_companiesgroupconstituentid", value); }
        }

        [AttributeLogicalNameAttribute("new_companiesgroupconstituentid")]
        public override Guid Id
        {
            get { return base.Id; }
            set { this.CompaniesGroupConstituentId = value; }
        }

        [AttributeLogicalNameAttribute("new_name")]
        public string Name
        {
            get { return this.GetAttributeValue<string>("new_name"); }
            set { this.SetAttributeValue<string>("new_name", "new_name", value); }
        }

        [AttributeLogicalNameAttribute("new_groupid")]
        public CrmEntityReference CompaniesGroupRef
        {
            get
            {
                // HACK to fix null reference exceptions in joins where CompaniesGroupRef would be null
                return this.GetAttributeValue<EntityReference>("new_groupid") ?? new CrmEntityReference { Id = Guid.NewGuid() };
            }
            set { this.SetAttributeValue<EntityReference>("new_GroupId", "new_groupid", value); }
        }

        [AttributeLogicalNameAttribute("new_groupid")]
        [RelationshipSchemaNameAttribute("new_new_companiesgroup_new_companiesgroupcons")]
        public CompaniesGroup CompaniesGroup
        {
            get { return this.GetRelatedEntity<CompaniesGroup>("new_new_companiesgroup_new_companiesgroupcons", null); }
            set { this.SetRelatedEntity<CompaniesGroup>("new_new_companiesgroup_new_companiesgroupcons", "new_new_companiesgroup_new_companiesgroupcons", null, value); }
        }

        [AttributeLogicalNameAttribute("new_constituentid")]
        public CrmEntityReference CompanyRef
        {
            get { return this.GetAttributeValue<EntityReference>("new_constituentid"); }
            set { this.SetAttributeValue<EntityReference>("new_ConstituentId", "new_constituentid", value); }
        }

        [AttributeLogicalNameAttribute("new_constituentid")]
        [RelationshipSchemaNameAttribute("new_new_company_new_companiesgroupconstituent")]
        public Company Company
        {
            get { return this.GetRelatedEntity<Company>("new_new_company_new_companiesgroupconstituent", null); }
            set { this.SetRelatedEntity<Company>("new_new_company_new_companiesgroupconstituent", "new_new_company_new_companiesgroupconstituent", null, value); }
        }

        /// <summary>
        /// Constructor for populating via LINQ queries given a LINQ anonymous type
        /// <param name="anonymousType">LINQ anonymous type.</param>
        /// </summary>
        public CompaniesGroupConstituent(object anonymousType) :
            this()
        {
            foreach (var p in anonymousType.GetType().GetProperties())
            {
                var value = p.GetValue(anonymousType, null);
                if (p.PropertyType == typeof(Guid))
                {
                    // Type is Guid, must be Id
                    base.Id = (Guid)value;
                    Attributes["new_companiesgroupconstituentid"] = base.Id;
                }
                else if (p.Name == "FormattedValues")
                {
                    // Add Support for FormattedValues
                    FormattedValues.AddRange((FormattedValueCollection)value);
                }
                else
                {
                    Attributes[p.Name.ToLowerInvariant()] = value;
                }
            }
        }
    }
}
